import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/mobile_app_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_pickers/pickers.dart';

class RegisterPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => RegisterPageState();
}

class RegisterPageState extends State<RegisterPage> {
  TextEditingController accountController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  TextEditingController rePasswordController = TextEditingController();
  TextEditingController jobController = TextEditingController();
  TextEditingController addrController = TextEditingController();
  int job = 0;

  int emailCodeCountdown = 0;


  void register() {
    if (accountController.text.isEmpty) {
      EasyLoading.showError("请输入账号");
      return;
    }
    if (passwordController.text.isEmpty) {
      EasyLoading.showError("请输入密码");
      return;
    }
    if (rePasswordController.text.isEmpty) {
      EasyLoading.showError("请再次输入密码");
      return;
    }
    if (passwordController.text != rePasswordController.text) {
      EasyLoading.showError("两次输入的密码不一致");
      return;
    }
    if (job == 0) {
      EasyLoading.showError("请选择身份");
      return;
    }
    if (addrController.text.isEmpty) {
      EasyLoading.showError("请输入所在地");
      return;
    }

    Api.mobileApp.register(
      account: accountController.text,
      password: passwordController.text,
      job: job,
      addr: addrController.text,
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget image1 = Container(
      decoration: const BoxDecoration(
          image:
              DecorationImage(image: AssetImage('assets/images/card-1.png'))),
    );

    Widget image2 = Container(
      decoration: const BoxDecoration(
          image:
              DecorationImage(image: AssetImage('assets/images/clock-2.png'))),
    );

    Widget text = Container(
      margin: const EdgeInsets.only(top: 250),
      child: Center(
        child: Text(
          "Register",
          style: TextStyle(
            color:
                Theme.of(context).textTheme.bodyMedium!.color!.withAlpha(200),
            fontSize: 40,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );

    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        title: const Text("注册"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Stack(
              children: <Widget>[
                Positioned(width: 380, height: 300, child: image1),
                Positioned(
                    left: 95, top: 50, width: 80, height: 250, child: image2),
                Positioned(
                  child: text,
                )
              ],
            ),
            Padding(
              padding: const EdgeInsets.all(30.0),
              child: Column(
                children: <Widget>[
                  Container(
                    padding: const EdgeInsets.all(5),
                    child: Column(
                      children: <Widget>[
                        TextField(
                          textInputAction: TextInputAction.next,
                          controller: accountController,
                          keyboardType: TextInputType.name,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(Icons.account_box),
                            hintText: "请输入账号",
                            labelText: "账号",
                          ),
                        ),
                        const SizedBox(height: 5),
                        TextField(
                          textInputAction: TextInputAction.next,
                          controller: passwordController,
                          keyboardType: TextInputType.visiblePassword,
                          obscureText: true,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(Icons.password),
                            hintText: "请输入密码",
                            labelText: "密码",
                          ),
                        ),
                        const SizedBox(height: 5),
                        TextField(
                          textInputAction: TextInputAction.next,
                          controller: rePasswordController,
                          keyboardType: TextInputType.visiblePassword,
                          obscureText: true,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(Icons.password),
                            hintText: "请输入确认密码",
                            labelText: "确认密码",
                          ),
                        ),
                        const SizedBox(height: 5),
                        TextField(
                          textInputAction: TextInputAction.none,
                          readOnly: true,
                          controller: jobController,
                          onTap: () {
                            Pickers.showSinglePicker(
                              context,
                              data: MobileAppModel.jobList,
                              onConfirm: (data, index) {
                                jobController.text = MobileAppModel.jobList[index];
                                job = index;
                              },
                            );
                          },
                          keyboardType: TextInputType.text,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(Icons.perm_identity),
                            hintText: "请选择身份",
                            labelText: "身份",
                          ),
                        ),
                        const SizedBox(height: 5),
                        TextField(
                          textInputAction: TextInputAction.next,
                          controller: addrController,
                          keyboardType: TextInputType.streetAddress,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(Icons.location_city),
                            hintText: "请输入所在地",
                            labelText: "所在地",
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 30),
                  ElevatedButton(
                    onPressed: () => register(),
                    child: const SizedBox(
                      height: 50,
                      child: Center(
                        child: Text(
                          "注册",
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(height: 10),
                  InkWell(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    borderRadius: BorderRadius.circular(5),
                    child: SizedBox(
                      height: 50,
                      child: Center(
                        child: Text(
                          "前往登录",
                          style: TextStyle(
                              color: Theme.of(context)
                                  .textTheme
                                  .bodyMedium!
                                  .color!
                                  .withAlpha(500),
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
